﻿/**
isUpload strange behaviour
Looks like there is some sticky state in Ext.Ajax.request?

I have two buttons on a form.

Button one does the below:

    Ext.Ajax.request({
    url: theURL + '.do',
    form: theForm,
    isUpload: true
    });

So the outgoing request goes as multipart (in fact the response is a csv file download).


Button two does the below:

    Ext.Ajax.request({
    url: theURL + '.do?x=a' + Math.round(Math.random() * 1000),
    isUpload:false,
    form: theForm,
    headers: {'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8','x-requested-with': 'XMLHttpRequest'},
    success: function(xhr) {
...

The strange thing if I click the second button directly, the response is proper but if I click it after first button, it is submitted as multipart instead of urlencoded -

as you can see I have even forced the headers but fiddler shows it as a wrong request (therefore the response data is braced with PRE tags and does not serve properly).

reference:http://extjs.com/forum/showthread.php?t=56864
 **/

Ext.override(Ext.data.Connection, {
    doFormUpload : function(o, ps, url){
        var id = Ext.id();
        var frame = document.createElement('iframe');
        frame.id = id;
        frame.name = id;
        frame.className = 'x-hidden';
        if(Ext.isIE){
            frame.src = Ext.SSL_SECURE_URL;
        }
        document.body.appendChild(frame);

        if(Ext.isIE){
           document.frames[id].name = id;
        }

        var form = Ext.getDom(o.form);
        var buf = {
            target: form.target,
            method: form.method,
            enctype: form.enctype,
            action: form.action
        };
        form.target = id;
        form.method = 'POST';
        form.enctype = form.encoding = 'multipart/form-data';
        if(url){
            form.action = url;
        }

        var hiddens, hd;
        if(ps){ // add dynamic params
            hiddens = [];
            ps = Ext.urlDecode(ps, false);
            for(var k in ps){
                if(ps.hasOwnProperty(k)){
                    hd = document.createElement('input');
                    hd.type = 'hidden';
                    hd.name = k;
                    hd.value = ps[k];
                    form.appendChild(hd);
                    hiddens.push(hd);
                }
            }
        }

        function cb(){
            form.target = buf.target;
            form.method = buf.method;
            form.enctype = form.encoding = buf.enctype;
            form.action = buf.action;
            var r = {  // bogus response object
                responseText : '',
                responseXML : null
            };

            r.argument = o ? o.argument : null;

            try { //
                var doc;
                if(Ext.isIE){
                    doc = frame.contentWindow.document;
                }else {
                    doc = (frame.contentDocument || window.frames[id].document);
                }
                if(doc && doc.body){
                    r.responseText = doc.body.innerHTML;
                }
                if(doc && doc.XMLDocument){
                    r.responseXML = doc.XMLDocument;
                }else {
                    r.responseXML = doc;
                }
            }
            catch(e) {
                // ignore
            }

            Ext.EventManager.removeListener(frame, 'load', cb, this);

            this.fireEvent("requestcomplete", this, r, o);

            Ext.callback(o.success, o.scope, [r, o]);
            Ext.callback(o.callback, o.scope, [o, true, r]);

            setTimeout(function(){Ext.removeNode(frame);}, 100);
        }

        Ext.EventManager.on(frame, 'load', cb, this);
        form.submit();

        if(hiddens){ // remove dynamic params
            for(var i = 0, len = hiddens.length; i < len; i++){
                Ext.removeNode(hiddens[i]);
            }
        }
    }
});